package com.task.manager.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

import com.task.manager.domain.Lock;

public interface LockMapper {
	@Delete({ "delete from t_lock", "where LOCK_CODE = #{lockCode,jdbcType=VARCHAR}" })
	int deleteByPrimaryKey(String lockCode);

	@Insert({ "insert into t_lock (LOCK_CODE, LOCK_TIME, ", "EXP_TIME)", "values (#{lockCode,jdbcType=VARCHAR}, #{lockTime,jdbcType=BIGINT}, ", "#{expTime,jdbcType=BIGINT})" })
	int insert(Lock record);

	@InsertProvider(type = LockSqlProvider.class, method = "insertSelective")
	int insertSelective(Lock record);

	@Select({ "select", "LOCK_CODE, LOCK_TIME, EXP_TIME", "from t_lock", "where LOCK_CODE = #{lockCode,jdbcType=VARCHAR}" })
	@Results({ @Result(column = "LOCK_CODE", property = "lockCode", jdbcType = JdbcType.VARCHAR, id = true), @Result(column = "LOCK_TIME", property = "lockTime", jdbcType = JdbcType.BIGINT),
			@Result(column = "EXP_TIME", property = "expTime", jdbcType = JdbcType.BIGINT) })
	Lock selectByPrimaryKey(String lockCode);

	@UpdateProvider(type = LockSqlProvider.class, method = "updateByPrimaryKeySelective")
	int updateByPrimaryKeySelective(Lock record);

	@Update({ "update t_lock", "set LOCK_TIME = #{lockTime,jdbcType=BIGINT},", "EXP_TIME = #{expTime,jdbcType=BIGINT}", "where LOCK_CODE = #{lockCode,jdbcType=VARCHAR}" })
	int updateByPrimaryKey(Lock record);

	@Insert({ "insert ignore into t_lock (LOCK_CODE, LOCK_TIME, ", "EXP_TIME)", "values (#{lockCode,jdbcType=VARCHAR}, #{lockTime,jdbcType=BIGINT}, ", "#{expTime,jdbcType=BIGINT})" })
	int insertIngnore(Lock record);

	@Select({ "select * from t_lock", "where LOCK_CODE = #{lockCode,jdbcType=VARCHAR}", "AND LOCK_TIME+EXP_TIME > #{now,jdbcType=BIGINT}" })
	Lock selectWithExpTime(@Param("lockCode") String lockCode, @Param("now") Long now);
}